package com.yozinga;

import android.os.Bundle;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;

import com.yozinga.model.BaseModel.OnRequestFinishedCallback;
import com.yozinga.model.RedeemOfferModel;
import com.yozinga.util.AlertDialogManager;
import com.yozinga.util.AppData;
import com.yozinga.util.ToolKit;

public class OfferDealPageRating extends BaseActivity implements OnClickListener,OnRequestFinishedCallback
{

	String description,offerId;
	float mRating;
	RedeemOfferModel redeemOfferModel;
	EditText etDesc;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_offerdealpage_rating);

		RatingBar ratingBar = (RatingBar) findViewById(R.id.rbItemList);
		
		mRating = ratingBar.getRating();

		Button submit = (Button) findViewById(R.id.submit);
		submit.setOnClickListener(this);
		//if rating value is changed,
		//display the current rating value in the result (textview) automatically
		ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
				mRating = rating;
			}
		});

		offerId = getIntent().getStringExtra("offerId");
		etDesc = (EditText)findViewById(R.id.comment);
		description = etDesc.getText().toString();
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.submit){

			ToolKit.startProgress(OfferDealPageRating.this, R.string.loading);
			 redeemOfferModel = new RedeemOfferModel(getApplicationContext());
			redeemOfferModel.addOnServiceRequestCompletedListener(this);
			
			String deviceId = Settings.Secure.getString(getContentResolver(), 
					Settings.Secure.ANDROID_ID);
			Log.i("Today","Device ID is: "+deviceId);
			Log.i("Today","Rating is: "+mRating);
			Log.i("Today","Desc is: "+etDesc.getText().toString());
			redeemOfferModel.rateOffer(AppData.OFFER_REVIEW, offerId, deviceId,etDesc.getText().toString(), mRating);
		}else{
			super.onClick(v);
		}

	}

	@Override
	public void onRequestFinished(int resultCode, Bundle payload) {
		ToolKit.stopprogess();
		AlertDialogManager al = new AlertDialogManager();
		al.showAlertDialog(getApplicationContext(), null, "Your Rating on Offer is Recorded", true, "OK");
	}

}
